package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> insertBy(@RequestBody SetmealDto setmealDto) {
        setmealService.insert(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page> pageR(Integer page, Integer pageSize, String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        Page<SetmealDto> page2 = new Page<>(page, pageSize);

        BeanUtils.copyProperties(page1, page2, "records");

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        setmealService.page(page1, lqw);
//        对象拷贝
        List<Setmeal> records = page1.getRecords();

        List<SetmealDto> collect = records.stream().map(item -> {
            SetmealDto sd = new SetmealDto();
            BeanUtils.copyProperties(item, sd);

            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId != null) {
                String name1 = byId.getName();
                sd.setCategoryName(name1);
            }
            return sd;
        }).collect(Collectors.toList());
        page2.setRecords(collect);

        return R.success(page2);
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteById(@RequestParam List<Long> ids) {
        setmealService.deleteBy(ids);
        return R.success("删除成功");
    }

    /**
     * 停售
     */
    @PostMapping("/status/{status}")
    public R<Setmeal> statusInsert(@PathVariable Integer status, @RequestParam("ids") List<Long> ids) {
        Setmeal byId=null;
        for (Long id : ids) {
            byId = setmealService.getById(id);
            byId.setStatus(status);
            setmealService.updateById(byId);
        }

        return R.success(byId);
    }
//    修改回显
    @GetMapping("/{id}")
    public R<Setmeal> setmealR(@PathVariable("id") Long id){
        Setmeal byId = setmealService.getById(id);
        return R.success(byId);
    }
//    客户端套餐
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+setmeal.status")
    public R<List<Setmeal>> selectCgd(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
